package com.wtwd.device.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.wtwd.device.constant.RespCode;
import com.wtwd.device.dao.BloodSugarRecordMapper;
import com.wtwd.device.model.entity.BloodSugarInfo;
import com.wtwd.device.model.entity.BloodSugarRecord;
import com.wtwd.device.model.vo.RespEntity;
import com.wtwd.device.service.BloodSugarRecordService;
import com.wtwd.device.util.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mjy
 * @date 2021/09/06
 * @description
 */
@Service
public class BloodSugarRecordServiceImpl implements BloodSugarRecordService {
    @Autowired
    private BloodSugarRecordMapper bloodSugarRecordMapper;

    @Override
    public RespEntity getDaysBloodSugarRecordList(Long deviceId, String startTime, String endTime) {
        List<BloodSugarRecord> bloodSugarRecords = bloodSugarRecordMapper.getDaysBloodSugarRecordList(deviceId, startTime, endTime);
        if (bloodSugarRecords != null && bloodSugarRecords.size() > 0) {
            bloodSugarRecords.forEach(bloodSugarRecord -> {
                bloodSugarRecord.setListBloodSugarRecord(JSONArray.parseArray(bloodSugarRecord.getBloodSugarInfo(), BloodSugarInfo.class));
            });
        }
        return new RespEntity(RespCode.SUCCESS, bloodSugarRecords);
    }

    @Override
    public RespEntity<List<BloodSugarRecord>> getMonthBloodSugarRecordList(Long deviceId, String month) {
        List<BloodSugarRecord> bloodSugarRecords = bloodSugarRecordMapper.getMonthBloodSugarRecordList(deviceId, month);
        if (bloodSugarRecords != null && bloodSugarRecords.size() > 0) {
            bloodSugarRecords.forEach(bloodSugarRecord -> {
                bloodSugarRecord.setListBloodSugarRecord(JSONArray.parseArray(bloodSugarRecord.getBloodSugarInfo(), BloodSugarInfo.class));
            });
        }
        return new RespEntity(RespCode.SUCCESS, bloodSugarRecords);
    }

    @Override
    public RespEntity addBloodSugarRecord(BloodSugarRecord bloodSugarRecord) {
        String time = DateUtils.format(bloodSugarRecord.getTime(), DateUtils.dateFormat5);
        BloodSugarRecord bloodSugarRecordOld = bloodSugarRecordMapper.getBloodSugarRecordByDeviceIdAndTime(bloodSugarRecord.getDeviceId(), time);
        if (bloodSugarRecordOld != null) {
            return new RespEntity(RespCode.TODAYS_DATA_ALREADY_EXIST);
        }
        bloodSugarRecord.setBloodSugarInfo(JSONArray.toJSONString(bloodSugarRecord.getListBloodSugarRecord()));
        bloodSugarRecord.setUploadTime(new Date().getTime());
        Integer rest = bloodSugarRecordMapper.addBloodSugarRecord(bloodSugarRecord);
        return new RespEntity(rest>0?RespCode.SUCCESS:RespCode.FAIL);
    }

    @Override
    public Integer clearBloodSugarRecordByDate(String date) {
        return bloodSugarRecordMapper.clearBloodSugarRecordByDate(date);
    }

    @Override
    public Integer deleteBloodSugarRecordByDeviceId(Long id) {
        return bloodSugarRecordMapper.deleteBloodSugarRecordByDeviceId(id);
    }

    @Override
    public BloodSugarRecord getDayBloodSugarRecord(Long deviceId, String day) {
        return bloodSugarRecordMapper.getBloodSugarRecordByDeviceIdAndTime(deviceId,day);
    }
}
